/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.digwener.ui;

/**
 *
 * @author Anton
 */
import javax.swing.*;
import javax.swing.table.*;
import com.digwener.entities.users.*;
import com.digwener.db.*;
import java.util.*;
public class RespList extends DefaultTableModel {
    JTable table = null;
    
    UserHolder userHolder =  UserHolder.getInstance();
    JTextField userTextField;
    
    public void setUserTextField(JTextField userTextField){
        this.userTextField = userTextField;
    }
    


    public String getFileName() {
        return this.userHolder.getFileName();
    }

    public void setFileName(String fileName) {
        this.userHolder.setFileName(fileName);
    }
     
    JTable getTable(){
        return table;
    }
    List<Boolean> marks = new ArrayList<Boolean>();
    protected String[] colName = {"N","Name","Selected"};
    
    protected Class[] colClass = new Class[]{Integer.class,String.class,Boolean.class};
    
    private void fillTable(){
        userHolder.loadFromXML();
        userTextField.setText(userHolder.getUserName());
    }
    
    public void saveToXML(){
        userHolder.setUserName(this.userTextField.getText());
        userHolder.saveToXML();
    }
    
    public void loadFromXML(){
        userHolder.loadFromXML();
        this.userTextField.setText(userHolder.getUserName());
    }
    
    public void loadFromDB(){
        RespTable respTable = new RespTable();
        respTable.setUserName(userTextField.getText());
        respTable.loadFromDB();
        UserType mergeable = respTable.getUserType();
        userHolder.merge(mergeable.getResponsibilities());
    }
    
    public void saveToDB(){
          RespTable respTable = new RespTable();
          respTable.setUserName(userTextField.getText());
          UserType saveable = userHolder.getUserElem(true);
          respTable.setUserType(saveable);
          respTable.saveToDB();
    }
    
    public RespList(JTextField userTextField, String userFileName){
        super(0,3);
        this.userTextField = userTextField;
        this.setFileName(userFileName);
        table = new JTable();
        table.setModel(this);
        table.setAutoCreateRowSorter(true);
        //table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table.getColumnModel().getColumn(0).setPreferredWidth(80);
        fillTable();
    }
    
    public String getUserName(){
        if(userHolder.getResps()==null){
            return "NULL";
        }
        return userHolder.getUserName();
    }    
    
    public void render(){
        table.setVisible(true);
        
        this.setRowCount(userHolder.getResps().size());
        marks.add(true);marks.add(true);
        
        table.repaint();
    }
    
    
    
    @Override
    public String getColumnName(int col){return colName[col];}
    @Override   
    public Class getColumnClass(int col){return colClass[col];}
    @Override
    public boolean isCellEditable(int row,int col){
        return true;// col == 2;
    }
    
    private String getRespName(int row){
        if (userHolder.getResps()!=null){
                        return userHolder.getResps().get(row).getRespName();
                        
                    }else{
                        return "NULL";
                    }
    }
    
    @Override
    public Object getValueAt(int row,int col)
	{
		switch(col)
		{
		case 0: 
                       return new Integer(row+1);
                       
                case 1:
                    if (userHolder.getResps()!=null){
                        return getRespName(row);
                        
                    }else{
                        return "NULL";
                    }
                    
                       
                case 2: if (userHolder.getResps()!=null){
                        return userHolder.isSelected(getRespName(row));
                        
                    }else{
                        return false;
                    }
		}
                return null;
		
	}
    @Override
    public void setValueAt(Object object, int row, int col){
       if (userHolder.getResps()!=null){
                        userHolder.setSelected(getRespName(row),((Boolean)object).booleanValue());
                        
                    }
    }
    
    public void selectAll(){
        userHolder.selectAll();
        render();
    }
    
    public void deselectAll(){
        userHolder.deselectAll();
        render();
    }
    
    public void deleteSelected(){
        userHolder.deleteSelected();
        render();
    }
    
}
